<template>
	<view class="full box relative bg-primary confirm-order-wrap flex column p-safe">
		<view class="relative flex-1 box w-100">
			<view class="absolute inset">
				<scroll-view class="full scroll-view" scroll-y>
					<view class="order-info-wrap box">
						<view class="order-info">
							<view class="text-desc">
								<view class="text-title">
									<text>{{ orderData.title }}</text>
								</view>
								<view class="order-desc-list">
									<!-- <view class="order-desc-item">
										<text>新疆XXX公路夜游（含餐食）</text>
									</view> -->
									<view class="order-desc-item" v-for="(item,index) in roomList" :key="index">
										<text class="hotel-name-text">{{ item.hotelName }}:</text>
										<text class="hotel-type-name-item" v-for="(typeItem,typeIndex) in item.children"
											:key="typeIndex">{{ typeItem.typeName }}x{{ typeItem.value }}</text>
									</view>
									<view class="order-desc-item">
										<text>{{ personNum }}</text>
									</view>
									<!-- <view class="order-desc-item" v-for="(item,index) in roomList" :key="index">
										<text>{{ item.hotelName }}: 房间数X{{ item.value }}</text>
									</view> -->
								</view>
							</view>
							<view class="order-image">
								<image :src="orderData.coverImage" class="full" mode="aspectFill"></image>
							</view>
						</view>
						<view class="line"></view>
						<view class="tip-list">
							<view class="tip-item">
								<text>1、本产品需二次预约，建议提前联系客服，本产品需二次预约，建议提前联系客服。</text>
							</view>
							<view class="tip-item">
								<text>2、法定节假日及前一天入住需补差价，详情请咨询客服。</text>
							</view>
						</view>
					</view>
					<view class="buy-info-wrap">
						<view class="header flex">
							<text>选择优惠券</text>
							<text class="coupon-text" @click="chooseCoupon">{{ activeCoupon }}</text>
						</view>
					</view>
					<view class="buy-info-wrap">
						<view class="header">
							<text>购买信息</text>
						</view>
						<view class="info-line"></view>
						<view class="info-list-wrap">
							<view class="info-item">
								<text class="info-label">联系人:</text>
								<input v-model="linkman_name" type="text" class="info-input" placeholder="联系人姓名" />
							</view>
							<view class="info-item">
								<text class="info-label">联系手机:</text>
								<input class="info-input" type="tel" v-model="linkman_tel" placeholder="用于接收通知短信" />
							</view>
							<view class="info-item">
								<text class="info-label">订单留言:</text>
								<input v-model="linkman_remake" class="info-input" placeholder="选填，付款后商家可见" />
							</view>
						</view>
					</view>
					<view class="confirm-info">
						<view class="header">
							<text>下单说明</text>
						</view>
						<view class="content">
							<text>下单说明下单说明下单说明下单说明下单说明下单说明下单说明下单</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="footer-wrapper w-100">
			<view class="order-amount">
				<text class="label">合计：</text>
				<text class="price-value">￥{{ basePrice }}</text>
			</view>
			<!-- <view class="pay-btn">
				<text>立即支付</text>
			</view> -->
			<u-button text="立即支付" @click="confirmHandle" />
		</view>
		<use-coupon @change="changeHandle" :activeValue="couponId" :total="totalPrice" :list="couponList"
			:visible="showPopup" @close="handPopup" />
	</view>
</template>

<script>
	import UseCoupon from './com/use-coupon.vue'
	import { mapState } from 'vuex'
	import { getOpenId, mpWxPay, wxPerPay } from '../order-util/index.js'
	export default {
		components: { UseCoupon },
		created() {
			console.log(this.orderData, 'orderData');
		},
		methods: {
			validateForm() {
				const { linkman_name, linkman_tel, linkman_remake } = this
				if (!linkman_name.length) return this.$toast({ title: '请输入联系人姓名' })
				if (!linkman_tel.length) return this.$toast({ title: '请输入联系方式' })
				return {
					linkman_name,
					linkman_tel,
					linkman_remake
				}
			},
			mpWxOrderPay(order_id) {
				wxPerPay(order_id, {
					fail: () => {
						uni.hideLoading()
						this.$toast({ title: '提交失败' })
					},
					success: opt => {
						mpWxPay(opt, order_id, {
							success: () => {
								uni.hideLoading()
								this.$toast({ title: '支付成功' }, () => {
									uni.redirectTo({
										url: '/pages-bundle/order-detail/order-detail?id=' +
											order_id
									})
								})
							},
							fail: () => {
								uni.hideLoading()
								this.$toast({ title: '支付失败' }, () => {
									uni.navigateTo({
										url: `/pages-bundle/order-detail/order-detail?id=${order_id}&type=confirm`
									})
								})
							}
						})
					}
				})

			},
			async createOrder(data) {
				const resp = await this.$request({
					url: "/Order/addOrder",
					data,
				})
				const { code, data: respData, msg } = resp
				if (code !== 1) {
					uni.hideLoading()
					return this.$toast({ title: msg })
				}
				const { order_id } = respData
				// #ifdef MP-WEIXIN
				return this.mpWxOrderPay(order_id)
				// #endif
			},

			confirmHandle() {
				const payload = this.validateForm()
				if (!payload) return
				const { currentCoupon, couponId, couponMoney, basePrice } = this
				const {
					adult_num,
					child_num,
					old_num,
					startTime,
					roomNumTotal,
					orderPrice,
					roomTotal,
					lineId,
					roomList,
					rowRoomData
				} =
				this.orderData
				const options = {
					...payload,
					coupon_id: couponId || 0,
					coupon_money: couponMoney,
					adult_num,
					child_num,
					old_num,
					start_time: startTime,
					room_num: roomNumTotal,
					pay_type: 20,
					order_price: basePrice,
					total_price: orderPrice * 1 + roomTotal * 1,
					line_id: lineId,
					room_list: rowRoomData
				}
				uni.showLoading({
					title: '正在提交',
					mask: true
				})
				this.createOrder(options)
			},
			changeHandle(ev) {
				this.couponId = ev
			},
			chooseCoupon() {
				if (!this.couponLen) return this.$toast({ title: '暂无优惠券' })
				this.handPopup(true)
			},
			handPopup(v) {
				this.showPopup = v
			}
		},
		computed: {
			couponMoney() {
				const { currentCoupon } = this
				if (!currentCoupon) return 0
				return currentCoupon.coupon
			},
			basePrice() {
				const { totalPrice, couponMoney } = this
				return totalPrice - couponMoney
			},
			totalPrice() {
				const { orderPrice, roomTotal } = this.orderData
				return orderPrice * 1 + roomTotal * 1
			},
			couponList() {
				return this.orderData.coupon || []
			},
			couponLen() {
				const { couponList } = this
				if (!couponList || !couponList.length) return 0
				return couponList.length
			},
			currentCoupon() {
				const { couponList, couponId } = this
				const target = this.couponList.find(item => item.id === couponId)
				return target
			},
			activeCoupon() {
				const { couponLen, couponId } = this
				const title = couponLen > 0 ? `共${couponLen}张` : '无可用'
				const baseTitle = `未选择优惠券(${title})`
				if (couponId === '') return baseTitle
				const { currentCoupon } = this
				if (!currentCoupon) return baseTitle
				return `已选择${currentCoupon.coupon}元优惠券`
			},
			...mapState(['orderData']),
			roomList() {
				return this.orderData.roomList || []
			},
			personNum() {
				const { adult_num, child_num, old_num } = this.orderData
				return `成人X${adult_num}，儿童X${child_num}，老人X${old_num}`
			},
		},
		data() {
			return {
				couponId: '',
				showPopup: false,
				linkman_name: '',
				linkman_remake: '',
				linkman_tel: '',
			}
		}
	}
</script>

<style lang="scss" scoped>
	.confirm-order-wrap {
		box-sizing: border-box;
		position: relative;

		.scroll-view {
			padding: 20rpx 32rpx 0;

			.confirm-info {
				margin-top: 32rpx;

				.content {
					font-size: 20rpx;
					color: rgba(0, 0, 0, 0.6);
					line-height: 32rpx;
					margin-top: 16rpx;
				}

				.header {
					height: 44rpx;
					font-size: 28rpx;
					color: rgba(0, 0, 0, 0.9);
					line-height: 44rpx;
				}
			}

			.buy-info-wrap {
				padding: 24rpx;
				box-sizing: border-box;
				background: #FFFFFF;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-top: 24rpx;

				.info-list-wrap {
					.info-item {
						display: flex;
						align-items: center;
						margin-bottom: 24rpx;

						&:last-child {
							margin-bottom: 0;
						}


						.info-input {
							height: 40rpx;
							font-size: 28rpx;
							color: rgba(0, 0, 0, 0.4);
							line-height: 40rpx;
							margin-left: 28rpx;
						}

						.info-label {
							height: 40rpx;
							font-size: 28rpx;
							color: rgba(0, 0, 0, 0.9);
							line-height: 40rpx;
						}
					}
				}

				.info-line {
					margin: 16rpx 0;
					background-color: #EEEEEE;
					width: 100%;
					height: 1px;
				}

				.header {
					height: 44rpx;
					font-size: 28rpx;
					color: rgba(0, 0, 0, 0.9);
					line-height: 44rpx;
					font-weight: bold;

					.coupon-text {
						margin-left: auto;
						color: #999;
					}
				}
			}
		}

		.order-info-wrap {
			padding: 24rpx;
			background-color: #FFF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;

			.line {
				background-color: #E5E6EB;
				width: 100%;
				height: 1px;
				margin: 16rpx 0;
			}

			.tip-list {
				.tip-item {
					font-size: 24rpx;
					color: #F38224;
					line-height: 40rpx;
					word-break: break-all;
					margin-bottom: 4rpx;

					&:last-child {
						margin-bottom: 0;
					}
				}
			}

			.order-info {
				display: flex;


				.text-desc {
					flex: 1;
					margin-right: 16rpx;

					.order-desc-list {
						margin-top: 4rpx;

						.order-desc-item {
							font-size: 24rpx;
							color: rgba(0, 0, 0, 0.6);
							line-height: 40rpx;
							margin-bottom: 4rpx;

							.hotel-type-name-item {
								margin-right: 4rpx;

								&:last-child {
									margin-right: 0;
								}
							}

							.hotel-name-text {
								margin-right: 8rpx;
							}

							&:last-child {
								margin-bottom: 0;
							}
						}
					}

					.text-title {
						font-size: 30rpx;
						color: rgba(0, 0, 0, 0.9);
						line-height: 52rpx;
						word-break: break-all;
						font-weight: bold;
					}

				}

				.order-image {
					width: 176rpx;
					height: 176rpx;
					border-radius: 16rpx 16rpx 16rpx 16rpx;
					flex-shrink: 0;
					margin-left: auto;
					overflow: hidden;
				}
			}
		}

		.footer-wrapper {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background-color: #FFF;
			padding: 42rpx 32rpx;

			::v-deep button {
				width: 392rpx;
				height: 76rpx;
				background: linear-gradient(135deg, #8BDCFF 0%, #E4FF68 100%);
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				color: rgba(0, 0, 0, 0.9);
				font-weight: bold;
				border: 0;
			}

			.pay-btn {
				width: 392rpx;
				height: 76rpx;
				background: linear-gradient(135deg, #8BDCFF 0%, #E4FF68 100%);
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				color: rgba(0, 0, 0, 0.9);
				font-weight: bold;
			}

			.order-amount {
				height: 56rpx;
				display: flex;
				align-items: center;
				white-space: nowrap;

				.price-value {
					font-size: 40rpx;
					color: #FF7433;
					font-weight: bold;
				}

				.label {
					font-size: 30rpx;
					color: rgba(0, 0, 0, 0.9);
				}

			}
		}
	}
</style>